/** 
 * @Description: 文件描述
 * @Author: 程前
 * @Date: 2025-02-17 14:52:44
 * @LastEditors: 程前
 * @LastEditTime: 2025-03-11 10:01:31
 */

import { Viewer, Cartesian3, GeometryInstance, PolygonHierarchy, Color, Primitive, EllipsoidSurfaceAppearance, Material, PolygonGeometry } from "cesium";
import Roaming from "../../modelMotion/interpolation/Roaming";
import { getAssetsFile } from "@/utils";
import { useDrawRiver } from "../../tool/analysis/flood/waterPrimitive";
import { SimWater } from "@/cesium/test/"


export default class Sea {
    private viewer: Viewer;
    private roaming: Roaming;
    private waterPrimitive: any = null;
    private waterImg: string;
    constructor(viewer: Viewer) {
        this.viewer = viewer
        this.waterImg = getAssetsFile("cesium/waterNormals.jpg");

    }

    public async initEvents() {
        // 添加水面效果
        // const waterPrimitive = this.createRiverPrimitive(100)
        // this.viewer.scene.primitives.add(waterPrimitive);
        // let positions = Cartesian3.fromDegreesArray([112.45, 18.17, 113.08, 18.79, 111.93, 19.524, 111.665, 18.833])
        // let polygon = new SimWater({
        //     positions: positions,
        //     // waterColor: 'rgba(22,45,59,1.0)',
        //     waterColor: 'rgba(3,32, 59,1.0)',
        //     alpha: 0.7,
        //     speed: 0.1,
        //     height: 0.5,
        //     freq: 0.2,
        //     // choppy: 3.0,
        // })
        // this.viewer.scene.primitives.add(polygon.primitive)

        // 获取path.json的数据后模型漫游
        this.roaming = new Roaming(this.viewer, { radar: true });
        // const res = await fetch('/interpolationPath.json')
        // const path = await res.json()
        // let arr = path.features[0].geometry.coordinates;
        // let lines = []
        // arr.forEach((item: number[]) => {
        //     lines.push(Cartesian3.fromDegrees(item[0], item[1], 1000))
        // })
        // this.roaming.modelRoaming({
        //     model: {
        //         uri: getAssetsFile('cesium/models/Cesium_Air.glb'),
        //         scale: 30,
        //         silhouetteSize: 2, //边框大小
        //         silhouetteColor: Color.fromCssColorString("#001aff"), //边框
        //     },
        //     Lines: lines,
        //     path: {
        //         show: true,
        //     },

        // })
        // 雷达路径
        const radarRes = await fetch('/RadarPos.json')
        const radarPath = await radarRes.json()

        // 船只路径
        const shipRes = await fetch('/TarPos.json')
        const shipPath = await shipRes.json()

        this.roaming.timeBasedModelRoaming(radarPath, {
            model: {
                uri: getAssetsFile('cesium/models/Cesium_Air.glb'),
                scale: 30,
                silhouetteSize: 2, //边框大小
                silhouetteColor: Color.fromCssColorString("#001aff"), //边框
            },
            path: {
                show: true,
            },
            radar: true,
            changeView: true,
        })
        const roaming2 = new Roaming(this.viewer, { radar: false });

        roaming2.timeBasedModelRoaming(shipPath, {
            model: {
                uri: getAssetsFile('cesium/models/heitiane.glb'),
                scale: 30,
                silhouetteSize: 2, //边框大小
                silhouetteColor: Color.fromCssColorString("#001aff"), //边框
            }
        })
    }

    /**
     * 创建指定高度水域
     * @param height 
     * @returns 
     */
    public createRiverPrimitive = (height: number) => {
        // 创建水面材质
        const riverMaterial = new Material({
            fabric: {
                type: "Water",
                uniforms: {
                    blendColor: new Color(0.0, 0.0, 1.0, 0.2),
                    normalMap: this.waterImg,
                    frequency: 500.0,
                    animationSpeed: 0.05,
                    amplitude: 10.0
                }
            }
        });
        const boundary = [112.45, 18.17, 113.08, 18.79, 111.93, 19.524, 111.665, 18.833]
        const polygon = this.useCreatePolygonGeometry(boundary, height);
        if (!polygon) return null;

        const primitive = new Primitive({
            geometryInstances: new GeometryInstance({
                geometry: polygon,
            }),
            appearance: new EllipsoidSurfaceAppearance({
                aboveGround: true,
            }),
            show: true,
            asynchronous: false,
            releaseGeometryInstances: false,
        });

        primitive.appearance.material = riverMaterial;
        return primitive;
    };

    private useCreatePolygonGeometry = (boundary: number[], extrudedHeight: number) => {
        try {
            // 验证参数
            if (!boundary || boundary.length < 6 || typeof extrudedHeight !== 'number' || isNaN(extrudedHeight)) {
                return null;
            }

            const polygon = new PolygonGeometry({
                polygonHierarchy: new PolygonHierarchy(Cartesian3.fromDegreesArray(boundary)),
                extrudedHeight,
                vertexFormat: EllipsoidSurfaceAppearance.VERTEX_FORMAT,
            });

            return polygon;
        } catch (error) {
            console.error(error);
            return null;
        }
    };

    public destroy() {
        this.roaming.destroy()
    }
}